package cn.cxion.chat.app.Fragment;

import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import cn.cxion.chat.R;
import cn.cxion.chat.app.adapter.OnHandAdapter;
import cn.cxion.chat.app.bean.MessageAction;
import cn.cxion.chat.app.bean.MessageEvent;
import cn.cxion.chat.app.bean.OnHandInfo;
import cn.cxion.chat.app.constant.Constant;
import cn.wildfirechat.message.Message;

public class OnHandFragment extends Fragment {


    @Bind(R.id.lv_hold)
    RecyclerView lvHold;
    private OnHandAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment_onhand, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        init();
        return view;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(MessageEvent message){
        if(message.getKey()== MessageAction.RefreshOnhand){
            refresh();
        }
    }
    public void refresh(){
        adapter.setList(Constant.getContractBeanList());
        adapter.notifyDataSetChanged();
        checkView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnTextChanged(value = R.id.et_search, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void search(Editable editable) {
        ToastUtils.showShort("搜索" + editable.toString());
    }

    private void init() {
         adapter = new OnHandAdapter(getContext());
        adapter.setList(Constant.getContractBeanList());
        lvHold.setLayoutManager(new LinearLayoutManager(getContext()));
        lvHold.setAdapter(adapter);
        checkView();
    }


    public void checkView(){
        if(adapter.getList().size()==0){
            lvHold.setVisibility(View.GONE);
        }else{
            lvHold.setVisibility(View.VISIBLE);
        }
    }

}
